#include "http_protocol/http_protocol_msg.h"
#include "http_protocol_operation.h"
#include "loop_module/loop.h"
#include "protocol_connection.h"
#include "protocol_operation.h"

#include <cstdint>
#include <cstdio>
#include <gtest/gtest.h>

TEST(HttpProtocolOp, InitTest)
{
}

TEST(HttpProtocolOp, TestUrl)
{
    std::string           url = "https://www.baidu.com/";
    Loop                  loop;
    HttpProtocolOperation operation(HttpProtocolOperation::OpMode::RESPONSE);
    ProtocolConnection    connection(&loop, &operation);
    connection.OnProtocolConnect([&connection](ProtocolConnection *, int result) {
        if (result >= 0)
        {
            printf("server connected.\n");
            HttpRequest request;
            auto        data = connection.ProtocolData();
            request.AddHeaderLine("GET", data.m_uri, "HTTP/1.1");
            request.AddHeaderSign("Connection", "keep-alive");
            request.AddHeaderSign("Accept", "*/*");
            std::string content;
            request.String(content);
            connection.SendMessage((const uint8_t *)content.c_str(), content.size());
        }
    });
    connection.OnProtocolMessage([&connection](ProtocolConnection *, ProtocolOperation * op) {
        printf("recv message.\n");
        auto response = ((HttpProtocolOperation *)op)->HttpResponseMsg();
        printf("%s %s.\n", response->GetCode().c_str(), response->GetNote().c_str());
        printf("body size : %ld.\n", response->Body().size());
        printf("%s..\n", (char*)response->Body().data());
        //connection.CloseConnection();
    });
    connection.OnProtocolDisConn([](ProtocolConnection * , int ) {

        printf("disconnection.\n");
    });
    connection.OnProtocolConnErr(



        [&connection](ProtocolConnection * conn, int result) { printf("connect state error result : %d.\n", result); });
    connection.InitServerUrl(url);
    loop.Run();
}
